package com.etc.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl;

/**
 * 数据库访问的通用类 2.0
 * 
 * @author Administrator
 *
 */
public class DBUtil {

	// 四个常量,处理数据库连接的必备参数
	private static final String DRIVER = "com.mysql.jdbc.Driver";
	private static final String URL = "jdbc:mysql://localhost:3306/jja1811";
	private static final String USER = "root";
	private static final String PASSWORD = "root";

	/**
	 * 返回一个连接对象
	 * 
	 * @return 一个java.sql.Connection对象
	 */
	public static Connection getConn() {
		Connection conn = null;
		try {
			Class.forName(DRIVER);
			conn = DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * 增加删除和修改的通用代码
	 * 
	 * @param sql
	 *            要执行的sql语句
	 * @param params
	 *            传递参数列表(是一个可变长度的参数)
	 * @return true 操作成功/false 操作失败
	 */
	public static boolean exUpdate(String sql, Object... params) {

		Connection conn = getConn();

		PreparedStatement pstmt = null;
		int n = 0;

		try {
			pstmt = conn.prepareStatement(sql);
			// 将占位符的值补充完整
			if (params != null) {
				for (int i = 0; i < params.length; i++) {
					pstmt.setObject(i + 1, params[i]);
				}

			}

			n = pstmt.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeAll(null, pstmt, conn);
		}
		return n > 0;

	}

	/**
	 * 简单的查询的方法
	 * 
	 * @param sql
	 * @param params
	 * @return CachedRowSet 离线的结果集
	 * 
	 */
	public static CachedRowSet exQuery(String sql, Object... params) {

		// 创建离线的结果集
		CachedRowSetImpl crs = null;
		try {
			crs = new CachedRowSetImpl();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		Connection conn = getConn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			// 将占位符的值补充完整
			if (params != null) {
				for (int i = 0; i < params.length; i++) {
					pstmt.setObject(i + 1, params[i]);
				}
			}
			rs = pstmt.executeQuery();

			// 将rs存放在CachedRowSet对象
			crs.populate(rs);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 这里不能做资源的释放
			closeAll(rs, pstmt, conn);
		}
		return crs;

	}

	/**
	 * 释放资源
	 * 
	 * @param rs
	 *            结果集
	 * @param pstmt
	 *            PreparedStatement对象
	 * @param conn
	 *            连接对象
	 */
	public static void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn) {

		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
